package com.tarena.lbs.coupon.service.impl;

import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.pojo.dto.AttachQrDTO;
import com.tarena.lbs.attach.pojo.param.AttachQRParam;
import com.tarena.lbs.coupon.dao.repository.CouponCodeRepository;
import com.tarena.lbs.coupon.pojo.query.UserCouponCodeQuery;
import com.tarena.lbs.coupon.service.CouponService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CouponServiceImpl implements CouponService {

    @DubboReference
    private AttachApi attachApi;
    @Autowired
    private CouponCodeRepository couponCodeRepository;
    public String receiveDetail(UserCouponCodeQuery query) {
        String code = query.getCouponCode();
        String key = "CouponCode:" + code;
        String value = couponCodeRepository.get(key);
        if (!"".equals(value) && value != null) {
            return value;
        }
        AttachQRParam param = new AttachQRParam();
        param.setContent(code);
        AttachQrDTO qrDTO = attachApi.generateQrCode(param);
        value = qrDTO.getUrl();
        couponCodeRepository.set(key, value, 30, TimeUnit.MINUTES);
        return qrDTO.getUrl();
    }
}
